using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QuanLyThuVien
{
    public partial class HoSoNhanVien : Form
    {
        public HoSoNhanVien()
        {
            InitializeComponent();
        }

        private void HoSoNhanVien_Load(object sender, EventArgs e)
        { 
            //lay tat ca cac laoi bang cap gan vao lisst
            List<LoaiBangCapDTO> list = LoaiBangCapBUS.laydanhsachloaibangcap_bus();
           
            //gan du lieu tu list qua cbbbangcap,load du lieu len
            cbbbangcap.DataSource = list;
          
            //gia tri hien thi len man hinh cho nguoi dung thay
            cbbbangcap.DisplayMember = "TenBangCap";

            //lay gia tri tai vi tri hien thi
            cbbbangcap.ValueMember = "MaBangCap";

            

            List<ChucVuDTO> list1 = ChucVuBUS.laydanhsachchucvu_bus();
            cbbchucvu.DataSource = list1;
            cbbchucvu.DisplayMember = "TenChucVu";
            cbbchucvu.ValueMember = "MaChucVu";


            List<BoPhanDTO> list2 = BoPhanBUS.laydanhsachbophan_bus();
            cbbbophan.DataSource = list2;
            cbbbophan.DisplayMember = "TenBoPhan"; //hien ten len man hinh
            cbbbophan.ValueMember = "MaBoPhan"; //

            
            List<NhanVienDTO> list3 = NhanVienBUS.laydanhsachnhanvien_bus();

            if (list3.Count > 0)
            {
                for (int i = 0; i < list3.Count; i++)
                {
                    int manhanvien = int.Parse(list3[i].Manhanvien.ToString());
                    string tennhanvien = list3[i].Hoten.ToString();
                    string diachi = list3[i].Diachi.ToString();
                    DateTime ngaysinh =DateTime .Parse ( list3[i].Ngaysinh.ToString());
                    string dienthoai = list3[i].Dienthoai.ToString();


                    int mabangcap = int.Parse(list3 [i].Bangcap .ToString ());
                    LoaiBangCapDTO lbc = LoaiBangCapBUS.laytentheoma_BUS(mabangcap);
                    string tenbangcap = lbc.Tenbangcap.ToString();

                    int mabophan = int.Parse(list3 [i].Bophan .ToString ());
                    BoPhanDTO bp = BoPhanBUS.laytentheoma_BUS(mabophan);
                    string tenbophan = bp.Tenbophan.ToString();

                    int machucvu = int.Parse(list3[i].Chucvu.ToString());
                    ChucVuDTO cv = ChucVuBUS.laytentheoma_BUS(machucvu );
                    string tenchucvu = cv.Tenchucvu.ToString();

                    string[] a = {manhanvien.ToString(), tennhanvien, ngaysinh.ToString(), diachi, dienthoai, tenbangcap, tenchucvu, tenbophan };
                    dgvnhanvien.Rows.Add(a);

                }
            }
            //dgvnhanvien.DataSource = list3;
        }

  
        //nhap vo trong datagv thi se hien hien len trong bnag thong tin nhan vien
        private void dgvnhanvien1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int mahientai = e.RowIndex;
                txthovaten.Text = dgvnhanvien.Rows[mahientai].Cells[1].Value.ToString();
                dtpngaysinh1.Text = dgvnhanvien.Rows[mahientai].Cells[2].Value.ToString();
                txtdiachi.Text = dgvnhanvien.Rows[mahientai].Cells[3].Value.ToString();
                txtdienthoai.Text = dgvnhanvien.Rows[mahientai].Cells[4].Value.ToString();
                cbbbangcap.Text = dgvnhanvien.Rows[mahientai].Cells[5].Value.ToString();
                cbbbophan.Text = dgvnhanvien.Rows[mahientai].Cells[6].Value.ToString();
                cbbchucvu.Text = dgvnhanvien.Rows[mahientai].Cells[7].Value.ToString();
            }
            catch
            {
            }
        }

       
       

        private void btthem_Click_1(object sender, EventArgs e)
        {
            string tennhanvien = txthovaten.Text;
            DateTime ngaysinh = DateTime.Parse(dtpngaysinh.Value.ToString());
            string diachi = txtdiachi.Text;
            string dienthoai = txtdienthoai.Text;
            int mabangcap = int.Parse(cbbbangcap.SelectedValue.ToString());
            int machucvu = int.Parse(cbbchucvu.SelectedValue.ToString());
            int mabophan = int.Parse(cbbbophan.SelectedValue.ToString());

            NhanVienDTO nv = new NhanVienDTO();
            nv.Hoten = tennhanvien;
            nv.Ngaysinh = ngaysinh;
            nv.Diachi = diachi;
            nv.Dienthoai = dienthoai;
            nv.Chucvu = machucvu;
            nv.Bophan = mabophan;
            nv.Bangcap = mabangcap;
            NhanVienBUS.themnhanvien_BUS(nv);

            // load lai len datagridview
            List<NhanVienDTO> list3 = NhanVienBUS.laydanhsachnhanvien_bus();
            dgvnhanvien.Rows.Clear();
            if (list3.Count > 0)
            {
                for (int i = 0; i < list3.Count; i++)
                {
                    int manhanvien = int.Parse(list3[i].Manhanvien.ToString());
                    string tennhanviencsdl = list3[i].Hoten.ToString();
                    string diachicsdl = list3[i].Diachi.ToString();
                    DateTime ngaysinhcsdl = DateTime.Parse(list3[i].Ngaysinh.ToString());
                    string dienthoaicsdl = list3[i].Dienthoai.ToString();
                    int mabangcapcsdl = int.Parse(list3[i].Bangcap.ToString());


                    LoaiBangCapDTO lbc = LoaiBangCapBUS.laytentheoma_BUS(mabangcapcsdl);
                    string tenbangcap = lbc.Tenbangcap.ToString();

                    int mabophancsdl = int.Parse(list3[i].Bophan.ToString());
                    BoPhanDTO bp = BoPhanBUS.laytentheoma_BUS(mabophancsdl);
                    string tenbophan = bp.Tenbophan.ToString();

                    int machucvucsdl = int.Parse(list3[i].Chucvu.ToString());
                    ChucVuDTO cv = ChucVuBUS.laytentheoma_BUS(machucvucsdl);
                    string tenchucvu = cv.Tenchucvu.ToString();

                    string[] a = { manhanvien.ToString(), tennhanviencsdl, ngaysinhcsdl.ToString(), diachicsdl, dienthoaicsdl, tenbangcap, tenchucvu, tenbophan };
                    dgvnhanvien.Rows.Add(a);

                }
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            MessageBoxEx.Show("lalal\ndong 2","thong bO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            DialogResult result = MessageBox.Show("Ban thuc su muon thoat", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.No)
            {
                return;
            }
            else
                this.Close();
        }

        private void btxoa_Click_1(object sender, EventArgs e)
        {
            try
            {
                int mahientai = dgvnhanvien.CurrentRow.Index;

                // xoa trong csdl
                string manv = dgvnhanvien.Rows[mahientai].Cells[0].Value.ToString();
                int manhanvien = int.Parse(manv.ToString());
                NhanVienBUS.xoanhanvien_BUS(manhanvien);
                dgvnhanvien.Rows.RemoveAt(mahientai);

            }
            catch
            {
            }
        }

        private void btsua_Click(object sender, EventArgs e)
        {

        }

        private void bttim_Click(object sender, EventArgs e)
        {
            string tennhanvien = txthovaten.Text;
            List<NhanVienDTO> list = NhanVienBUS.timnhanvientheoten_BUS(tennhanvien);
            dgvnhanvien.Rows.Clear();
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    int manhanvien = int.Parse(list[i].Manhanvien.ToString());
                    string tennhanviencsdl = list[i].Hoten.ToString();
                    string diachicsdl = list[i].Diachi.ToString();
                    DateTime ngaysinhcsdl = DateTime.Parse(list[i].Ngaysinh.ToString());
                    string dienthoaicsdl = list[i].Dienthoai.ToString();
                    int mabangcapcsdl = int.Parse(list[i].Bangcap.ToString());


                    LoaiBangCapDTO lbc = LoaiBangCapBUS.laytentheoma_BUS(mabangcapcsdl);
                    string tenbangcap = lbc.Tenbangcap.ToString();

                    int mabophancsdl = int.Parse(list[i].Bophan.ToString());
                    BoPhanDTO bp = BoPhanBUS.laytentheoma_BUS(mabophancsdl);
                    string tenbophan = bp.Tenbophan.ToString();

                    int machucvucsdl = int.Parse(list[i].Chucvu.ToString());
                    ChucVuDTO cv = ChucVuBUS.laytentheoma_BUS(machucvucsdl);
                    string tenchucvu = cv.Tenchucvu.ToString();

                    string[] a = { manhanvien.ToString(), tennhanviencsdl, ngaysinhcsdl.ToString(), diachicsdl, dienthoaicsdl, tenbangcap, tenchucvu, tenbophan };
                    dgvnhanvien.Rows.Add(a);

                }
            }
        }

        private void btlammoi_Click(object sender, EventArgs e)
        {
            txthovaten.Text = "";
            txtdienthoai.Text = "";
            txtdiachi.Text = "";
            dtpngaysinh1.Text = "";
            cbbbangcap.Text = "";
            cbbbophan.Text = "";
            cbbchucvu.Text = "";
        }

        private void grhosonhanvien_Click(object sender, EventArgs e)
        {

        }

      
        
       

       

        
 

    }
}